<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Notifications extends MY_Controller {

	//var $data = array('username'=>'Ruby', 'password'=>'123456', 'fullname'=>'Ruby Nguyen', 'image'=>'ruby.jpg', 'role'=>1);
	public function __construct()
	{
		parent::__construct();
		$this->load->model('Users_model');
		$this->load->model('Devices_model');
	}

	public function index()
	{
		// get data
		//	$this->getAll();
		echo "Hello";

	}
	public function getRequest($id)
	{
		// get data
		$json_data = array();
		$data1 = $this->Users_model->getById($id);
		if(!empty($data1)){
			$json_data['status'] = "1";
			if($data1[0]['role']==1)$json_data['admin']="true";
			else $json_data['admin']="true";
		}else {
			$json_data['status'] = "0";
			$json_data['message'] = "Data is empty";
		}
		$this->print_json($json_data);
	}

	/**
	 * Sending Push Notification
	 */
	public function sendNotification() {

		
			//get registration_id list from imei list
		//list_device[] , call_ring , capture , update_location
		$imei = $this->input->post('list_device');
		$call_ring = $this->input->post('call_ring');
		$capture = $this->input->post('capture');
		$update_location = $this->input->post('update_location');
		$lock_device = $this->input->post('lock_device');


		$registration_id = $this->Devices_model->getListRegistration_id($imei);
		//var_dump($registration_id);

		$message = $this->input->post('message');


		//send notification

		$data = array(
			'message'=>$message,
			'call_ring'=>$call_ring,
			'capture'=>$capture,
			'update_location'=>$update_location,
			'lock_device'=>$lock_device
			);
		
		$result = $this->postNotification($registration_id,$data);

		$json_data['status'] = "1";
		$json_data['message'] = $result;
		$this->print_json($json_data);
		

	}

	// send notification ios
	public function sendNotificationiOS() {

		
	    //get registration_id list from imei list
		//list_device[] , call_ring , capture , update_location
		$imei = $this->input->post('list_device');
		$message = $this->input->post('message');
		$registration_id = $this->Devices_model->getListRegistration_id($imei);
		//var_dump($registration_id);
		//send notification
		foreach ($registration_id as $key) {
		$result = $this->postNotificationiOS($key,$message);
			
		}

		$json_data['status'] = "1";
	if($result)		$json_data['message'] = "Send message successfully";
	else $json_data['message'] = "Can not send message";
		$this->print_json($json_data);
		

	}
	// Register
	public function register(){
		$imei = $this->input->post('imei');
		$registration_id = $this->input->post('registration_id');
		if($this->Devices_model->isExistEmei($imei)){

			if($this->Devices_model->update_registration_id($imei,$registration_id)){
				$json_data['status'] = "1";
				$json_data['message'] = "Update registration id successfully!";
			}else{
				$json_data['status'] = "0";
				$json_data['message'] = "Can not update registration id.";
			}
		}else{
			$json_data['status'] = "0";
			$json_data['message'] = "Imei does not exist.";
		}
		$this->print_json($json_data);
	}
}